/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uwntek.tcpSocket;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.uwntek.base.des.DesSecurity;
import com.uwntek.base.xmlToBean.XmlToBean;



/**
 * EchoServerThread中新建线程来处理客户端请求 EchoThread作为这样的处理线程的基类，主要是把socket通信部分再封装一下
 * 在run函数中把客户端请求的消息交给ClientRequestHandler来处理，本类只负责socket的消息接收和发送
 */
public class EchoThread extends Thread {

	protected PrintStream out;
	protected String message;
	protected Socket clientSocket;

	public EchoThread(Socket clientSocket) {
		super();
		try {

			// 接受数据，但不允许有中文，因为会乱码
			DataInputStream in = new DataInputStream(clientSocket
					.getInputStream());
			byte[] buffer = new byte[10000]; // 缓冲区的大小
			in.read(buffer); // 处理接收到的报文，转换成字符串

			/**
			 * C++传递过来的中文字，需要转化一下。C++默认使用GBK。
			 * GB2312是GBK的子集，只有简体中文。因为数据库用GB2312，所以这里直接转为GB2312
			 * */
			message = new String(buffer, "GBK").trim();
			System.out.println("客户端信息" + message);
			String decode = null;
			try {
				DesSecurity des = new DesSecurity("uwntek", "12345678");
				decode = des.decryptStr(message);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("after descrypMessage:" + decode);

			XmlToBean xmlToBean = new XmlToBean();
			xmlToBean.xmlToBean(decode);

			this.clientSocket = clientSocket;
			out = new PrintStream(clientSocket.getOutputStream());
		} catch (IOException ex) {
			Logger.getLogger(EchoServerThread.class.getName()).log(
					Level.SEVERE, null, ex);
			try {
				clientSocket.close();
			} catch (IOException ex1) {
				Logger.getLogger(EchoServerThread.class.getName()).log(
						Level.SEVERE, null, ex1);
			}
		}
	}

	public void run() {
		try {
			byte[] responseBuffer = new ClientRequestHandler(message)
					.response().getBytes("GB2312");
			// System.out.println("responseBuffer:"+responseBuffer);
			out.write(responseBuffer, 0, responseBuffer.length);

		} catch (Exception ex) {
			Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE,
					null, ex);
		}
		try {
			clientSocket.close();
		} catch (IOException ex) {
			Logger.getLogger(EchoThread.class.getName()).log(Level.SEVERE,
					null, ex);
		}
	}

	public String getMessage() {
		return message;
	}

	/**
	 * 获取输出流
	 * 
	 * @return
	 */
	public PrintStream getOutputStream() {
		return out;
	}
}
